package com.wzq.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wzq.controller.admin.GetSome;
import com.wzq.dao.dto.MemberDto;
import com.wzq.dao.pojo.*;
import com.wzq.service.IMemberService;
import com.wzq.service.IMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yzc
 * @since 2022-08-31
 */
@RestController
@RequestMapping("/member")
public class MemberController extends GetSome {

    @Autowired
    private IMemberService memberService;

    //   添加
    @PostMapping
    public Result save(@RequestBody Member member) {
        LambdaQueryWrapper<Message> lqw = new LambdaQueryWrapper();
        lqw.eq(Message::getType,3).eq(Message::getIsdelete,0).eq(Message::getIsread,0);
        lqw.eq(Message::getW_id,member.getW_id()).eq(Message::getP_id,member.getP_id());
        if (super.iMessageService.getOne(lqw)!=null)
            return new Result(Code.SAVE_ERR,null,"已申请");
        boolean flag = iMemberService.save(member);
        return new Result(flag ? Code.SAVE_OK:Code.SAVE_ERR,flag);
    }

    //   创建活动时
    @PostMapping("/{w_id}/add/{p_id}")
    public Result save(@RequestBody List<String>[] lists,@PathVariable Integer p_id,@PathVariable Integer w_id) {
//        添加创建者
        Member member = new Member();
        member.setW_id(w_id);
        member.setP_id(p_id);
        member.setIdentities(0);
        member.setIsaccpt(1);
        iMemberService.save(member);
//        添加普通成员
        member.setIdentities(1);
        for (String id : lists[1]){
            member.setW_id(Integer.valueOf(id));
            member.setP_id(p_id);
            if (!iMemberService.save(member)){
                return new Result(Code.SAVE_ERR,false);
            }
        }
        return new Result(Code.SAVE_OK,true);
    }

    @GetMapping("/select")
    public Result selectApp(@RequestBody List<Member> members,@RequestParam("like") String like) {
        ArrayList<Member> memberslist = new ArrayList<>();
        String pattern= "*"+like+"*";
        for (Member member : members) {
            if (Pattern.matches(pattern, member.getP_name()));
            memberslist.add(member);
        }
        return new Result(Code.GET_OK,memberslist,"true");
    }

    //添加创建者成员
    @GetMapping("AddmMember")
    public Result AddmMember(@RequestParam("w_id") String w_id ,@RequestParam("p_id") String p_id){
        System.out.println(w_id + p_id);
        int i = memberService.AddmMember(w_id,p_id);
        if (i>0){
            return new Result(Code.SAVE_OK,"创建者插入成功");
        }
        return new Result(Code.SAVE_ERR,"创建者插入失败");
    }

    //根据项目ID查询该项目下的成员信息
    @PostMapping("SelectMenberByPid")
    public Result SelectMenberByPid(@RequestBody MemberDto member){
        LambdaQueryWrapper<Member> lqw = new LambdaQueryWrapper();
        lqw.orderByAsc(Member::getIdentities);
        lqw.eq(Member::getIsaccpt,1).eq(Member::getP_id,member.getP_id());
        List<Member> list = iMemberService.list(lqw);
        ArrayList<Member> members = new ArrayList<>();
        for (Member m : list  ) {
            m.setW_name(getW_name(m.getW_id()));
            m.setSys_number(iWechat_userService.getById(m.getW_id()).getSys_number());
            m.setAvatarUrl(getAvatarUrl(m.getW_id()));
            members.add(m);
        }
//        List<MemberDto> list = iMemberService.SelectMenberByPid(member.getP_id());
        if (list.isEmpty()){
            return new Result(Code.GET_ERR,false,"查询失败");
        }
        return new Result(Code.GET_OK,members,"查询成功");
    }
    //  接收邀请  w_id 操作用户（收信人） p_id 加入项目  m_id 操作消息id
    @PutMapping("/{w_id}/accpt/{p_id}/read/{m_id}")
    public Result accpt(@PathVariable Integer w_id, @PathVariable Integer p_id, @PathVariable Integer m_id) {
        //  接受邀请
        LambdaQueryWrapper<Member> lqw = new LambdaQueryWrapper();
        lqw.eq(Member::getW_id,w_id).eq(Member::getP_id,p_id);
        Member member = iMemberService.getOne(lqw);
        member.setIsaccpt(1);
        boolean flag = iMemberService.updateById(member);
        Message message = iMessageService.getById(m_id);
        //  已读
        message.setIsread(1);
        message.setOperation(1);
        boolean flag2 = iMessageService.updateById(message);
        // 判断消息类型  是邀请他人加入  还是申请加入  活动
        Message message1=new Message();
        message1.setP_id(message.getP_id());
        message1.setW_id(w_id);
        message1.setW_id_down(message.getW_id());
        message1.setName(getW_name(w_id));
        if (message.getType()==3){
            message1.setType(10);
        }else if(message.getType()==1){
            message1.setType(13);
        }
        boolean save = iMessageService.save(message);

        return new Result(flag && flag2 && save ? Code.UPDATE_OK:Code.UPDATE_ERR,flag && flag2 && save);
    }

    //   拒绝
    @DeleteMapping("/{w_id}/unaccpt/{p_id}/read/{m_id}")
    public Result unaccpt(@PathVariable Integer w_id, @PathVariable Integer p_id, @PathVariable Integer m_id) {
        //  修改消息拒绝标记
        Message message = iMessageService.getById(m_id);
        message.setIsread(1);
        message.setOperation(2);
        boolean flag = iMessageService.updateById(message);
        // 删除 申请成员
        LambdaQueryWrapper<Member> lqw = new LambdaQueryWrapper();
        lqw.eq(Member::getW_id,w_id).eq(Member::getP_id,p_id);
        boolean flag2 = iMemberService.remove(lqw);
        // 根据消息类型 给发送者返回拒绝消息
        Message message1=new Message();
        message1.setP_id(message.getP_id());
        message1.setW_id(w_id);
        message1.setW_id_down(message.getW_id());
        message1.setName(getW_name(w_id));
        if (message.getType()==3){
            message1.setType(11);
        }else if(message.getType()==1){
            message1.setType(14);
        }
        boolean save = iMessageService.save(message);
        return new Result(flag && flag2 && save ? Code.UPDATE_OK:Code.UPDATE_ERR,flag && flag2 && save);
    }

    //   申请加入
    @PostMapping("Applyjoin")
    public Result Applyjoin(@RequestBody Member member) {
        boolean flag = iMemberService.save(member);
        return new Result(flag ? Code.SAVE_OK:Code.SAVE_ERR,flag);
    }

    //     获得申请理由
    @GetMapping("/app/{w_id}")
    public Result getById(@PathVariable Integer w_id) throws Exception {
        isover();
        List<Member> active = new ArrayList<>();

        LambdaQueryWrapper<Member> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Member::getW_id,w_id);
        List<Member> list = iMemberService.list(lqw);
        for (Member member : list) {
            Project byId = iProjectService.getById(member.getP_id());
            if (byId.getIsover()==0) {
                member.setP_name(getP_name(member.getP_id()));
                active.add(member);
            }
        }
        Integer code = active != null ? Code.GET_OK : Code.GET_ERR;
        String msg = active != null ? "" : "数据查询失败，请重试！";
        return new Result(code,active,msg);
    }

    //   修改
    @PutMapping
    public Result update(@RequestBody Member member) {
        boolean flag = iMemberService.updateById(member);
        return new Result(flag ? Code.UPDATE_OK:Code.UPDATE_ERR,flag);
    }
    //    删除
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable int id ) {
        boolean flag = iMemberService.removeById(id);
        return new Result(flag ? Code.DELETE_OK:Code.DELETE_ERR,flag);
    }
    //    删除多个id对应用户
    @DeleteMapping
    public Result deleteById(@RequestBody List<Integer> ids) {
        boolean flag = iMemberService.removeByIds(ids);
        return new Result(flag ? Code.DELETE_OK:Code.DELETE_ERR,flag);
    }
    //     获得所有成员
    @GetMapping
    public Result getAll() {
        List<Member> list = iMemberService.list();
        Integer code = list != null ? Code.GET_OK : Code.GET_ERR;
        String msg = list != null ? "" : "数据查询失败，请重试！";
        return new Result(code,list,msg);
    }

    @GetMapping("{w_id}/join/{p_id}")
    public Result IsJoin(@PathVariable Integer w_id,@PathVariable Integer p_id){
        LambdaQueryWrapper<Member> lqm = new LambdaQueryWrapper<Member>();
        lqm.eq(Member::getW_id,w_id).eq(Member::getP_id,p_id);
        Member one = iMemberService.getOne(lqm);
        if (one==null){
            return new Result(Code.GET_OK,3,"可申请");
        }

        if ( one.getIsaccpt()==1 ){
            return new Result(Code.GET_OK,1,"已加入该活动！");
        }else if( one.getIdentities()==0 ) {
            return new Result(Code.GET_OK,2,"无法申请,因该角色为创建者！");
        }
        return new Result(Code.GET_OK,3,"可申请");
    }
    //  是否以申请
    @PostMapping("IsApply")
    public Result IsApply(@RequestBody Member member){
        LambdaQueryWrapper<Project> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Project::getId,member.getP_id());
        wrapper.eq(Project::getApply,0);
        Project one = iProjectService.getOne(wrapper);
        if (one != null){
            return new Result(Code.GET_ERR,"不允许加入");
        }
        LambdaQueryWrapper<Message> lqw = new LambdaQueryWrapper();
        lqw.eq(Message::getType,3).eq(Message::getIsdelete,0).eq(Message::getIsread,0);
        lqw.eq(Message::getW_id,member.getW_id()).eq(Message::getP_id,member.getP_id());
        if (super.iMessageService.getOne(lqw)!=null)
            return new Result(Code.SAVE_ERR,null,"已申请");
        return new Result(Code.SAVE_OK,null,"未申请");
    }

}

